/* MODEL of the Model-View-Controller
 */

package textselection;

import java.util.LinkedList;
import java.util.List;

/**
 * @author Jason Frank
 */
public class TextSelectionModel {

  private int strPointSize;
  private int x;  // coord of the "mouse" position (provided on cmd line)
  private int y;
  private String strs[]; // ptr only

  // observers is a list of Views that want to be notified of changes to the model
  List<TextView_Interface> observers = new LinkedList<TextView_Interface>();

  // constructor
  public TextSelectionModel(int strPointSize, String strs[] ) {

    this.strPointSize = strPointSize;
    //this.x = x;
    //this.y = y;
    this.strs = strs;
  } /////// end constructor ////

  // Method adds a View to list of Observers to be notified of changes to the model
  public void addObserver(TextView_Interface view) { // (each view will call this to add itself)
    this.observers.add(view);
  } /// end addObserver() //////

  // Delete a char from one of the strs.  Deletes the "char_index" char.
  public void delete_char( int str_index, int char_index ) {

    String str = strs[str_index];
    String before_del_char = str.substring(0, char_index); // [0, char_index)
    String after_del_char  = str.substring(char_index +1, str.length()); // [char_index+1,end]
    strs[str_index] = before_del_char + after_del_char;

  } /////// end delete_char() ////////////

  // Insert a new char into the string indicated by str_index, at position "position"
  public void insert_char( int str_index, String new_char_str, int position) {

    String str = strs[str_index];
    String before_char = str.substring(0, position); // [0, position)
    String after_char  = str.substring(position, str.length()); // [position,end]
    strs[str_index] = before_char + new_char_str + after_char;
    
  } ///// end insert_char() /////////////////

  ///// GETTERS and SETTERS ///////
  
  public void setXY(int x, int y) {
    int old_x = this.x;
    int old_y = this.y;
    this.x = x;
    this.y = y;
    // Notify all views of the change
    for (TextView_Interface view : observers) {
      view.curValChanged(old_x, old_y);
    }
  } 

  public int getX() {   return this.x;  }
  //public void setMaxValue(int maxValue) {  this.maxValue = maxValue; }
  public int getY() {   return this.y;  }
  //public void setMinValue(int minValue) {  this.minValue = minValue; }
  public int getStrPointSize() {   return strPointSize; }
  public String[] getStrs() {      return strs;  }
  public int getNumStrs()   {      return strs.length; }

  @Override
  public String toString() {
    String str = new String();
    str += "\n  String Point Size: " + this.strPointSize;
    str += "\n  X: " + this.x;
    str += "\n  Y: " + this.y;
    str += "\n  Strings: " + java.util.Arrays.toString(this.strs);
    return str;
  } //// end toString() ////

} ////////// end class TextSelectionModel ///////////////////
